package cn.ml.ssmdemo.controller;

import cn.ml.ssmdemo.entity.Student;
import cn.ml.ssmdemo.service.StudentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
//@RestController  //所有方法返回字符串，方法上不用加@ResponseBody
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public ModelAndView list(){
        List<Student> students = studentService.list(null);
        Map<String,Object> map = new HashMap<>();
        map.put("students",students);
        return new ModelAndView("listStudent",map);
    }
    @RequestMapping("/list2")
    //public String list(ModelMap map){
    public String list2(Model map){
        map.addAttribute("students",studentService.list(null));
        return "listStudent";
    }

    //传递json格式数据，此格式转换只能转换此方法下的，应该再xml中配置，可以转换所有的json
    @RequestMapping(value = "/list3",produces = "application/json;charset=utf-8")
    @ResponseBody  //返回json数据必须加此注解,不会走视图解析器，直接返回字符串
    public List<Student> list3(){
        List<Student> students = studentService.list(null);
        return students;
    }
    @RequestMapping(value = "/list4",produces = "application/json;charset=utf-8")
    @ResponseBody  //返回json数据必须加此注解,不会走视图解析器，直接返回字符串
    public String list4(){
        List<Student> studentList = new ArrayList<>();
        Student stu1 = new Student(1,"雷布斯1","男");
        Student stu2 = new Student(2,"雷布斯2","女");
        Student stu3 = new Student(3,"雷布斯3","男");
        Student stu4 = new Student(4,"雷布斯4","女");
        studentList.add(stu1);
        studentList.add(stu2);
        studentList.add(stu3);
        studentList.add(stu4);

        System.out.println("************java对象 转 JSON字符串****************");
        String str1 = JSON.toJSONString(studentList);
        System.out.println("JSON.toJSONString(list)==>"+str1);
        String str2 = JSON.toJSONString(stu1);
        System.out.println("Json.toJSONString(student1)==>"+str2);

        System.out.println("************JSON字符串 转 java对象****************");
        Student jp_student1 = JSON.parseObject(str2,Student.class);
        System.out.println("JSON.parseObject(str2,Student.class)==>"+jp_student1);

        System.out.println("************java对象 转 JSON对象****************");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(stu2);
        System.out.println("(JSONObject) JSON.toJSON(student2)==>"+jsonObject1);

        System.out.println("************JSON对象 转 java对象****************");
        Student to_java_stu = JSON.toJavaObject(jsonObject1,Student.class);
        System.out.println("JSON.toJavaObject(jsonObject1,Student.class)==>"+to_java_stu);

        return str1;
    }

    @RequestMapping("/show")
    //@RequestParam("student_id")前台提交的参数(与后台接收不一致时使用)，且使用后前台必须提交
    //@RequestParam(value = "student_id",required = false) true-前台必须提交(默认true)  false-非必须提交
    //public String show(@RequestParam(value = "student_id",required = false) Integer id, ModelMap map){
    public String show(Integer id,ModelMap map){
        map.addAttribute("student",studentService.get(id));
        return "showStudent";
    }

    @GetMapping("/add")
    //@RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "addStudent";
    }
    @PostMapping("/add")
    //@RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Student student){
        studentService.save(student);
        return "redirect:show?id="+student.getId(); //重定向到show
    }

    @GetMapping("/update")
    public String update(Integer id,ModelMap map){
        map.addAttribute("student",studentService.get(id));
        return "updateStudent";
    }
    @PostMapping("/update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:show?id="+student.getId();
    }
    @GetMapping("/delete")
    public String delete(Integer id){
        studentService.deleteById(id);
        return "redirect:list";  //重定向到show
    }

}
